package com.clzw.module_activity.viewmodel

import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.ActivityPersonListBean
import com.czl.base.data.bean.NoticeListBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriber200Helper
import com.czl.base.util.RxThreadHelper

class EnlistsViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
//    var currentPage = 0
    var activityId=""
    val uc = UiChangeEvent()

    class UiChangeEvent {
        val refreshListEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val loadCompleteEvent: SingleLiveEvent<ActivityPersonListBean> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getEnlist()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
//        currentPage = 0
        getEnlist()
    })
    fun getEnlist(){
        model.getPersonListForActivity(activityId).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<ActivityPersonListBean>>(){
                override fun onResult(t: BaseBean<ActivityPersonListBean>) {
//                        currentPage++
                    uc.loadCompleteEvent.postValue(t.data)
                    toolbarRightText.set(if (t.data.isNullOrEmpty()) "共0人" else "共${t.data!!.size}人")
                }
                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }
            })
    }
}